﻿namespace IBTrader
{
    using IBTrader.Charts;
    using IBTrader.Indicator;
    using IBTrader.Model;
    using IBTrader.Modules;
    using IBTrader.Modules.Configuration;
    using IBTrader.Modules.Log;
    using IBTrader.Modules.Tws;
    using System;
    using System.Linq;

    class Console
    {
        public static void Main(string[] args)
        {
            new Console();
        }
        public event EventHandler<Modules.Market.Order.EndArgs> End;

        public event EventHandler<EventArgs> Load, Unload, Connected;
        public Console()
        {
            System.Console.WriteLine("Console");
            var objects = Creator.Create(ComponentsSection.Components.Console, this);
            var IB = Creator.Select<IIB>(objects, "ib");
            var worker = Creator.Select<Worker>(objects);
            var Store = Creator.Select<Store>(objects, "store");

            if (Load != null) Load(this, EventArgs.Empty);

            var Algo = Store.Algos.SingleOrDefault();
            if (Algo == null)
            {
                Algo = new Algo(Store);
                Store.Algos.Add(Algo);
            }
            Algo.PriceOffset = 0.1;
            Algo.StopLimit = 1.0;
            Algo.BacklogConfiguration.Folder = @"D:\TickData\Futures.Live";
            Algo.BacklogConfiguration.FileFormat = @"{0} {1:yyyy-MM-dd}.{2}";

            var total = 0.0;
            for (DateTime dt = new DateTime(2011, 1, 1); dt < new DateTime(2013, 1, 1); dt = dt.AddDays(1) )
            //var dt = new DateTime(2011, 1, 3); 
            {
                Algo.Clear();
                worker.Filters.OfType<BasePrices>().ToList().ForEach(c => c.Prices.Clear());

                Algo.BacklogConfiguration.Date = dt;
                worker.Reader.Do(Algo.BacklogConfiguration);
                var summary = Store.Orders.GetSummary();
                total += summary.Summ;
                System.Console.WriteLine("{0,22} {1} {2:0.000}", dt, summary, total);
                if (System.Console.KeyAvailable) break;
            }

            if (Unload != null) Unload(this, EventArgs.Empty);
        }
        public void Redraw(object _, EventArgs __)
        {
        }
        public virtual void Log(object o, LogTarget.LogArgs e)
        {
        }
    }
}